package cn.tedu.boot21.controller;

import cn.tedu.boot21.entity.Emp;
import cn.tedu.boot21.entity.User;
import cn.tedu.boot21.mapper.EmpMapper;
import cn.tedu.boot21.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/reg")
    public String reg(User user){
        System.out.println("user = " + user);
        //通过用户名查询
        User u=mapper.selectByUsername(user.getUsername());
        if(u!=null){
            return "用户名已经存在！";
        }

        mapper.insert(user);
        return "注册成功！";
    }


    @RequestMapping("/login")
    public String login(User user){
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if(u.getPassword().equals(user.getPassword())){
                return "登陆成功！";
            }
        }
        return "用户名或密码错误！";
    }

    @RequestMapping("/regA")
    public int regA(@RequestBody User user){
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            return 2;//代表"用户名已存在！"
        }
        mapper.insert(user);
        return 1;//代表成功
    }

    @RequestMapping("/loginA")
    public int loginA(@RequestBody User user){
        User u = mapper.selectByUsername(user.getUsername());

        if(u==null){
            return 2;//代表"用户名不存在！"
        }

        if(!u.getPassword().equals(user.getPassword())){
            return 3;//代表密码错误
        }
        return 1;

    }




}
